﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using System.Drawing;
using System.Threading;
using Google.Protobuf.WellKnownTypes;


namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// ERP发运确认
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ERP_OMShipOracleERPController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string gz_con;


        public ERP_OMShipOracleERPController(ILogger<ERP_OMShipOracleERPRequestDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            gz_con = _configuration.GetConnectionString("GZ_WMS_DbConnection");
            _logger = loggerFactory.CreateLogger<ERP_OMShipOracleERPController>();

        }

        /// <summary>
        ///  ERP发运确认
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostOMShipOracleERP([FromBody] ERP_OMShipOracleERPRequestDto strparm)
        {
            string  strMsg = "";
            object log = "";
            log = strparm;
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            ERP_OMShipOracleERPResponseDto re = new ERP_OMShipOracleERPResponseDto();                      
            try
            {
                if (strMsg == "")
                {
                    ERP_TokenRequestDto user_info = new ERP_TokenRequestDto();
                    Dictionary<string, string> urldic = new Dictionary<string, string>(); //URL获取dic
                    urldic.Add("function_description", "ERP账号鉴权");//查看URL配置表地址描述
                    DataTable url_dt = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                    if (url_dt.Rows.Count > 0)
                    {
                        //user_info.userName = "9999";
                        //user_info.passWord = "Gokin@123";
                        user_info.userName = "CRM_OCR";
                        user_info.passWord = "Gokinocr@123";

                        string jsonText = JsonConvert.SerializeObject(user_info);
                        var reAgv = DB.httpPost(url_dt.Rows[0]["interface_url"].ToString(), jsonText, "", "否");
                        ERP_TokenResponseDto psp = JsonConvert.DeserializeObject<ERP_TokenResponseDto>(reAgv);
                        if (psp.statusCode != 200)
                        {
                            strMsg = "ERP鉴权失败 ERR:" + psp.message;
                        }
                        else
                        {
                            urldic = new Dictionary<string, string>(); //查询公用dic
                            urldic.Add("function_description", "ERP发运确认");//查看URL配置表地址描述
                            DataTable url_dt2 = MethodHelp.GetTableInfo("TMS0004", urldic, con);//获取URL地址
                            if (url_dt2.Rows.Count > 0)
                            {

                                string jsonText2 = JsonConvert.SerializeObject(strparm);
                                var reAgv2 = DB.httpPost_Erp(url_dt2.Rows[0]["interface_url"].ToString(), jsonText2, psp.data.ToString(), "是");
                                if (reAgv2.ToString().Contains("ERROR")|| reAgv2.ToString().Contains("Scux_Sie_Soa_Pub_Transfer_Utl"))
                                {
                                    strMsg = reAgv2.ToString();
                                }
                                else
                                {
                                    re.message= reAgv2.ToString();
                                }                               
                            }
                        }

                    }

                }               
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }
            if (strMsg=="")
            {
                re.statusCode = 0;
                //re.message = "过账成功";
            }
            else
            {
                re.statusCode = 500;
                re.message = "ERP ERROR:" + strMsg;
            }

            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);
        }

      
    }
}
